import {canvas,ctx} from './canvas'
import draw from './draw.js'
let dr,keyDown,speed,score

dr=new draw();

window.requestAnimFrame=(function(){
    return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
            return window.setTimeout(callback, 1000 / 60);
        };
})()

document.body.onload=game()

function game(){
    init()
    gameLoop()
}
function init(){
    dr.init();
    dr.ImageDraw()
}
function gameLoop(){
    dr.ImageDraw()
    requestAnimFrame(gameLoop)
}

//按键事件
keyDown={}
window.addEventListener('keydown',function(e){
    e=event||window.event
    let zh;
    let capsLockKey = e.keyCode ? e.keyCode : e.which
    let shifKey = e.shiftKey ? e.shiftKey:((capsLockKey == 16) ? true : false)
    if(((capsLockKey >= 65 && capsLockKey <= 90) && !shifKey)||((capsLockKey >= 97 && capsLockKey <= 122) && shifKey)){
        zh=true
    }else{
        zh=false
    }
    keyDown[e.keyCode]=true;
    for(let i=0;i<dr.le.length;i++){
        if(zh==true){
            if(String.fromCharCode(e.keyCode).toLocaleLowerCase()==dr.le[i].lett){
                dr.born(i)
            }
        }
        else{
            if(String.fromCharCode(e.keyCode)==dr.le[i].lett){
                dr.born(i)
            }
        }        
    }
});
window.addEventListener('keyup',function(e){
    delete keyDown[e.keyCode];//释放按键对象
});				

